package servlet.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SetCookie2Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public SetCookie2Servlet() {

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 쿠키값으로 name-한글이름을 전송
		// 한글의 경우 Encoding처리가 필요함.
		String name =URLEncoder.encode("김철수","UTF-8");
		System.out.println(name);
		// 쿠키 생성
		Cookie c1 = new Cookie("name", name);
		int age = 33;
		// String ageStr = String.valueOf(age); //스트링 변환
		Cookie c2 = new Cookie("age", age + "");

		// 쿠키 전송
		response.addCookie(c1);
		response.addCookie(c2);

		// 응답
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("<html><body>");
		out.println("쿠키 값 설정한 이름과 나이 : " + name + "-" + age + "<br>");
		out.println("<a href='/myservlet/cookie/get_cookie.html'>쿠키 값 조회</a>");
		out.println("</body></html>");

	}

}
